#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int pd[10002][202], a[10002][202];
int main()
{

    int n, m , i,j,x,l=0,ans = 0;
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++)
    {
        l = 0;
        for (j = 0; j < m; j++)
        {
            scanf("%d%d", &pd[i][j], &a[i][j]);
            if(pd[i][j]==1) l++;
        }
        pd[i][m] = l;
    }
    scanf("%d", &x);
    i = 1;
    while(i<=n)
    {
        ans+= a[i][x];
        ans%=20123;
        int k=0;
        for (j=x; ; j++)
        {
            if(j==m) j=0;
            
            if(pd[i][j] == 1) k++;
            if(k==(a[i][x]-1)%pd[i][m]+1) break;
        }
        x = j;
        i++;
    }
    printf("%d", ans);
    return 0;
}


